import { useMutation, useQuery } from "react-query";
import { useRequest } from "../utils/request";
import { IPage, IPageParams } from "../types";

/**
 * !搜索文章接口
 * @returns
 */
export const useGetArticle = (queryParams: IArticleSearchParams) => {
  const request = useRequest<IPage<IArticle>>();
  return useQuery(["getArticleBySearch", queryParams], () =>
    request("/article/ua/getArticleBySearch", { data: queryParams })
  );
};

/**
 * !添加文章接口
 */
export const useAddArticle = () => {
  const request = useRequest<string>();
  return useMutation(async (params: IAddArticle) => {
    const result = request("/article/addArticle", {
      data: params,
      method: "PUT",
    });
    return result;
  });
};

/**
 * !修改文章接口
 */

export const useUpdateArticle = () => {
  const request = useRequest<string>();
  return useMutation(async (params: IUpdateArticle) => {
    const result = request("/article/updateArticleById", {
      data: params,
      method: "POST",
    });
    return result;
  });
};

/**
 * !删除文章接口
 */
export const useDeletedArticle = () => {
  const request = useRequest<string>();
  return useMutation(async (params: { articleId: number }) => {
    const result = request("/article/deletedArticleById", {
      data: params,
      method: "DELETE",
    });
    return result;
  });
};

/**
 * 搜查参数类型
 */
export type IArticleSearchParams = {
  articleTitle: string;
  recommend: number | null;
} & IPageParams;

/**
 * 添加文章类型
 */
export type IAddArticle = {
  articleTitle: string;
  articleText: string;
  unitName: string;
  recommend: number;
  articleBanner: string;
};

/**
 * 添加文章类型
 */
export type IUpdateArticle = {
  articleId: number;
} & Partial<IAddArticle>;

export type IArticle = {
  /**
   * 文章id
   */
  articleId: number;
  /**
   * 文章标题
   */
  articleTitle: string;
  /**
   * 文章内容
   */
  articleText: string;
  /**
   * 文章标题图片
   */
  articleBanner: string;
  /**
   * 所属单位
   */
  unitName: string;
  /**
   * 创建人ID(管理员)
   */
  createSysId: number;
  /**
   * 是否推荐; 1=是;2=否;
   */
  recommend: number;
  /**
   * 观看次数
   */
  watchTime: number;

  /**
   * 创建时间
   */
  createTime: string;

  /**
   * 更新时间
   */
  updateTime: string;
};
